//
//  DependenceViewController.m
//  UIPickerViewDemo
//
//  Created by Justsoso on 16/2/24.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "DependenceViewController.h"

@interface DependenceViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic,strong) NSDictionary *stateZips;
@property (nonatomic,strong) NSArray *states;
@property (nonatomic,strong) NSArray *zips;

@end

@implementation DependenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *path = [[NSBundle mainBundle] pathForResource:@"statedictionary" ofType:@"plist"];
    NSLog(@"%@",path);
    self.stateZips = [NSDictionary dictionaryWithContentsOfFile:path];
    
    self.states = [[self.stateZips allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
    NSString *curState = self.states[0];
    self.zips = self.stateZips[curState];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnPressed:(id)sender
{
    NSInteger sel1 = [self.pickerView selectedRowInComponent:0];
    NSInteger sel2 = [self.pickerView selectedRowInComponent:1];
    self.infoLabel.text = [NSString stringWithFormat:@"当前选中的是:%@城市的%@邮编",self.states[sel1],self.zips[sel2]];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}


// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return self.states.count;
    }
    else
    {
        return self.zips.count;
    }
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {
        return self.states[row];
    }
    else
    {
        return self.zips[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)
    {
        NSString *curState = self.states[row];
        self.zips = self.stateZips[curState];
        
        [self.pickerView reloadComponent:1];
        
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
    }
    else
    {
        self.infoLabel.text = [NSString stringWithFormat:@"邮编是:%@",self.zips[row]];
    }
}
@end
